/*
 * @Description: 我的审核reducer
 * @Author: Rfan
 * @Date: 2023-02-10 11:12:31
 * @LastEditTime: 2023-02-10 16:28:20
 */

import {
  HIDE_APPROVE_DIALOG,
  SET_APPROVE_LIST,
  SET_APPROVE_NAV,
  SET_LIST_QUERY,
  SHOW_APPROVE_DIALOG,
  SUBMIT_APPROVE
} from '../constants';
import { selectItemByKey, updateItemById } from '@/utils/list';
import { EnumTaskState } from '@/view/use/MyApprovals/dictionaries';

export const MyApprovalsReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 设置请求参数
     */
    case SET_LIST_QUERY: {
      return { ...state, listQuery: action.data };
    }

    /**
     * @description: 设置利用审批列表
     * @param {*} data 列表数据
     */
    case SET_APPROVE_LIST:
      return {
        ...state,
        approveList: action.data
      };

    /**
     * @description: 设置利用审批导航
     * @param {*} data 列表数据
     */
    case SET_APPROVE_NAV:
      return {
        ...state,
        approveNav: action.navKey
      };

    /**
     * @description: 打开查看申请弹窗
     */
    case SHOW_APPROVE_DIALOG:
      return {
        ...state,
        approveDialog: {
          visible: true,
          data: action.data,
          type: action.dialogType
        }
      };

    /**
     * @description: 打开查看申请弹窗
     */
    case HIDE_APPROVE_DIALOG: {
      const { id } = action;
      const item = selectItemByKey(id, state.approveList, 'processInstanceId');
      item.taskState = EnumTaskState.Completed;
      return {
        ...state,
        approveList: updateItemById(id, item, state.approveList, 'processInstanceId'),
        approveDialog: {
          visible: false,
          data: null,
          type: ''
        }
      };
    }

    /**
     * @description: 打开查看申请弹窗
     */
    case SUBMIT_APPROVE:
      return {
        ...state,
        approveDialog: {
          visible: false,
          data: null
        }
      };

    default:
      console.log('untouched', action);
      return state;
  }
};
